package com.zhuoding.dao;

import com.zhuoding.domain.Student;
import com.zhuoding.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class StudentDao {

    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    //学生登录功能
    public Student login(String sid,String spassword){
        String sql = "select * from student where sid = ? and spassword = ?";
        try {
            Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), sid, spassword);
            return student;
        }catch (Exception e){
            new RuntimeException(e);
        }
        return null;
    }

    //根据学号sid去查询学生信息
    public Student findBySid(String sid){
        String sql = "select * from student where sid = ?";
        try {
            Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), sid);
            return student;
        }catch (Exception e){
            new RuntimeException(e);
        }
        return null;
    }
    //保存学生信息方法
    public int addStudent(Student student){
        String sql = "insert into student values (?,?,?,?,?,?)";
        int row = jdbcTemplate.update(sql, student.getSid(), student.getName(), student.getSpassword(), student.getSex(), student.getAge(), student.getMajor());
        return row;
    }
}
